﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 录入可选值管理
//最后修改: 李城 2010-10-12 新增获取可选值 树形结构 
using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;
using System.Data;
using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace TRPOP.CMS.DALSQLServer
{
    public class FormInputValueDAL : IFormInputValueDAL
    {

        #region 保存 Save
        public EnumSubmitResult Save(FormInputValueInfo info)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_" + "FormInputValue_Save";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);

            odbDataBase.AddInParameter(odbCommand, "@FormInputValueTypeId", System.Data.DbType.Int32, info.FormInputValueTypeId);
            odbDataBase.AddInParameter(odbCommand, "@ParentId", System.Data.DbType.Int32, info.ParentId);
            odbDataBase.AddInParameter(odbCommand, "@Name", System.Data.DbType.String, info.Name);
            odbDataBase.AddInParameter(odbCommand, "@Status", System.Data.DbType.Int32, info.Status);
            odbDataBase.AddInParameter(odbCommand, "@DisplayOrder", System.Data.DbType.Int32, info.DisplayOrder);
            odbDataBase.AddParameter(odbCommand, "@FormInputValueId", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, info.FormInputValueId);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                info.FormInputValueId = (int)odbDataBase.GetParameterValue(odbCommand, "@FormInputValueId");
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }

        #endregion

        #region 获取实体 GetInfoById
        public FormInputValueInfo GetInfoById(int id)
        {
            string strSqlString = "SELECT * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_FormInputValue WHERE FormInputValueId=" + id;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            FormInputValueInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    if (reader.Read())
                    {
                        oInfo = new FormInputValueInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }
        #endregion

        #region 删除 DeleteInfoById
        public EnumSubmitResult DeleteInfoById(int id)
        {
            return DeleteInfoByIds(id.ToString());
        }
        #endregion

        #region 删除 DeleteInfoByIds
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            string strSqlString = "DELETE FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_FormInputValue WHERE FormInputValueId in(" + ids + ")";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);

            EnumSubmitResult enmResult = EnumSubmitResult.Failed;
            try
            {
                odbDataBase.ExecuteNonQuery(odbCommand);
                enmResult = EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return enmResult;
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        public List<FormInputValueInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            string strSqlString = "SELECT ";
            if (topn > 0)
            {
                strSqlString += " TOP " + topn;
            }
            strSqlString += " * FROM " + SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_FormInputValue WHERE 1=1"; if (condition != "")
            {
                strSqlString += condition;
            }
            if (orderby != "")
            {
                strSqlString += " ORDER BY " + orderby;
            }
            else
            {
                strSqlString += " ORDER BY FormInputValueId DESC";
            }

            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetSqlStringCommand(strSqlString);
            List<FormInputValueInfo> olstInfo = new List<FormInputValueInfo>(); FormInputValueInfo oInfo = null;
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new FormInputValueInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        public List<FormInputValueInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().PagerStoreProcedureName;
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);
            List<FormInputValueInfo> olstInfo = new List<FormInputValueInfo>(); FormInputValueInfo oInfo = null;
            odbDataBase.AddInParameter(odbCommand, "@TableName", System.Data.DbType.String, SystemInfoHelper.getSystemInfo().DBprefixTableName + "_model_FormInputValue");
            odbDataBase.AddInParameter(odbCommand, "@ShowColumns", System.Data.DbType.String, "*");
            odbDataBase.AddInParameter(odbCommand, "@pageNumber", System.Data.DbType.Int32, pageNumber);
            odbDataBase.AddInParameter(odbCommand, "@pageSize", System.Data.DbType.Int32, pageSize);
            odbDataBase.AddInParameter(odbCommand, "@condition", System.Data.DbType.String, condition);
            odbDataBase.AddInParameter(odbCommand, "@orderby", System.Data.DbType.String, orderby);
            odbDataBase.AddOutParameter(odbCommand, "@recordCount", System.Data.DbType.Int32, 0);
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new FormInputValueInfo();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                }
                recordCount = int.Parse(odbCommand.Parameters["@RecordCount"].Value.ToString());
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion

        #region 获取可选值 树形结构

        /// <summary>
        /// 获取可选值的树形结构
        /// </summary>
        /// <param name="FormInputValueTypeId">可选值类型id</param>
        /// <param name="ParentId">可选值的父id</param>
        /// <param name="level">预设的级数</param>
        /// <param name="Condition">外加的条件</param>
        /// <returns></returns>
        public List<FormInputValueInfoWithLevel> GetFormInputValueListTree(int FormInputValueTypeId, int ParentId, int level, string Condition)
        {
            string strStoreProcedure = SystemInfoHelper.getSystemInfo().DBprefixTableName + "_FormInputValue_GetListTree";
            SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
            DbCommand odbCommand = odbDataBase.GetStoredProcCommand(strStoreProcedure);
            List<FormInputValueInfoWithLevel> olstInfo = new List<FormInputValueInfoWithLevel>();
            FormInputValueInfoWithLevel oInfo = null;
            odbDataBase.AddInParameter(odbCommand, "@FormInputValueTypeId", System.Data.DbType.Int32, FormInputValueTypeId);
            odbDataBase.AddInParameter(odbCommand, "@ParentId", System.Data.DbType.Int32, ParentId);
            odbDataBase.AddInParameter(odbCommand, "@level", System.Data.DbType.Int32, level);
            odbDataBase.AddInParameter(odbCommand, "@Condition", System.Data.DbType.String, Condition);
            try
            {
                using (IDataReader reader = odbDataBase.ExecuteReader(odbCommand))
                {
                    while (reader.Read())
                    {
                        oInfo = new FormInputValueInfoWithLevel();
                        ModelHelper.LoadInfoData(oInfo, reader);
                        olstInfo.Add(oInfo);
                    }
                    while (reader.NextResult())
                    {
                        while (reader.Read())
                        {
                            oInfo = new FormInputValueInfoWithLevel();
                            ModelHelper.LoadInfoData(oInfo, reader);
                            olstInfo.Add(oInfo);
                        }
                    }
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return olstInfo;
        }
        #endregion 
    }
}
